from typing import Optional
from asyncio import Queue

async def _send_status(status_queue: Optional[Queue], tool_name: str, message_type: str, data: dict):
    """辅助函数：向状态队列发送消息。"""
    if status_queue:
        await status_queue.put({
            "type": message_type,
            "step": tool_name,
            "data": data
        })

async def query_asset_info(ip_address: str, status_queue: Optional[Queue] = None) -> dict:
    """
    查询内部资产信息 (CMDB)。
    这是一个占位符实现，需要替换为真实的数据库或API查询。
    """
    tool_name = "Internal_Asset_Query"
    await _send_status(status_queue, tool_name, "tool_start", {"ip_address": ip_address})
    
    # --- 模拟数据 --- #
    # 在真实环境中，这里应该是连接到CMDB数据库或API的代码
    mock_cmdb = {
        "10.1.1.5": {"owner": "核心业务部", "hostname": "prod-db-01", "is_critical": True, "os": "Linux"},
        "10.1.2.10": {"owner": "研发部", "hostname": "dev-server-22", "is_critical": False, "os": "Windows Server"},
        "192.168.1.100": {"owner": "办公网", "hostname": "printer-lobby", "is_critical": False, "os": "PrinterOS"}
    }
    # --- 模拟结束 --- #

    if ip_address in mock_cmdb:
        result = mock_cmdb[ip_address]
        await _send_status(status_queue, tool_name, "tool_end", {"ip_address": ip_address, "result": result})
        return {"source": "Internal-CMDB", "data": result, "status": "success"}
    else:
        error_msg = "未在CMDB中找到该资产"
        await _send_status(status_queue, tool_name, "tool_error", {"ip_address": ip_address, "error": error_msg})
        return {"source": "Internal-CMDB", "error": error_msg, "status": "not_found"}
